﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class consultas_monitoreo : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["Ruc"] != "1")
            {
                this.lblRuc.Visible = false;
                this.lblRazonSocial.Visible = false;
                this.txtRazonSocial.Visible = false;
                this.txtRuc.Visible = false;
            }

            DateTime year = DateTime.Now;

            DateTime yearFinal = year.AddYears(5);
            DateTime yearInicial = year.AddYears(-5);
            DropDownListTextFiller.FillDropDownList(ddlDiaInicio, 1, 31, year.Day);
            DropDownListTextFiller.FillDropDownList(ddlMesInicio, 1, 12, year.Month);
            DropDownListTextFiller.FillDropDownList(ddlAnhoInicio, yearInicial.Year, yearFinal.Year, year.Year);

            DropDownListTextFiller.FillDropDownList(ddlDiaFin, 1, 31, year.Day);
            DropDownListTextFiller.FillDropDownList(ddlMesFin, 1, 12, year.Month);
            DropDownListTextFiller.FillDropDownList(ddlAnhoFin, yearInicial.Year, yearFinal.Year, year.Year);
        }

    }
    protected void btnConsultar_Click(object sender, EventArgs e)
    {

        if (IsValidStringParameter(txtRazonSocial.Text)) Alert.Show("Cliente");


        FilterDataBinder filterdatabinder = new FilterDataBinder();

        filterdatabinder.AddFilterData("Fecha", new DropDownList[] { ddlDiaInicio, ddlMesInicio, ddlAnhoInicio }, WhereFilter.WhereOperation.MoreEqualThan, WhereFilter.SQLValueType.String);
        filterdatabinder.AddFilterData("Fecha", new DropDownList[] { ddlDiaFin, ddlMesFin, ddlAnhoFin }, WhereFilter.WhereOperation.LessEqualThan, WhereFilter.SQLValueType.String);
        filterdatabinder.AddFilterData("Ruc", txtRuc, WhereFilter.WhereOperation.Equal, WhereFilter.SQLValueType.String);
        filterdatabinder.AddFilterData("Dni", txtDni, WhereFilter.WhereOperation.Equal, WhereFilter.SQLValueType.String);

        filterdatabinder.AddFilterData("Cliente", txtNombre, WhereFilter.WhereOperation.Like, WhereFilter.SQLValueType.String);
        filterdatabinder.AddFilterData("Cliente", txtRazonSocial, WhereFilter.WhereOperation.Like, WhereFilter.SQLValueType.String);

        filterdatabinder.BindData();



        SelectSentenceBuilder selecSentenceBuilder = new SelectSentenceBuilder("vw_MonitoreoTickets");

        selecSentenceBuilder.Filters = filterdatabinder.Filters;

        selecSentenceBuilder.Build();

        string select = selecSentenceBuilder.GetBuiltSelect();

        Session["SelectCommand"] = select;
        Session["Fecha_ini"] = FilterDataBinder.GetDateFromDownDropLists(ddlDiaInicio, ddlMesInicio, ddlAnhoInicio).Value.ToShortDateString();

        Session["Fecha_Fin"] = FilterDataBinder.GetDateFromDownDropLists(ddlDiaFin, ddlMesFin, ddlAnhoFin).Value.ToShortDateString();

        Response.Redirect("RMonitoreoTickets.aspx");
    }
    private bool IsValidStringParameter(string value)
    {
        value.Trim();
        return value.Length == 0 ? false : true;
    }
    protected override void OnError(EventArgs e)
    {

        HttpContext ctx = HttpContext.Current;

        Exception exception = ctx.Server.GetLastError();
        string errorInfo;
        if (exception is DateException)
            errorInfo = "<table align=\"center\"><tr><td><br><span style=\"color:red\">Se ingresó una o más fechas inválidas</span></br></td></tr></table>";
        else
            errorInfo =
                "<br>Offending URL: " + ctx.Request.Url.ToString() +
                "<br>Source: " + exception.Source +
                "<br>Message: " + exception.Message +
                "<br>Stack trace: " + exception.StackTrace;

        ctx.Response.Write(errorInfo);


        ctx.Server.ClearError();

        base.OnError(e);
    }





}
